// JavaScript Document

/*****************************************************
 * Tag Bars
 *****************************************************/
$(document).ready(
	function()
	{
		$('#tagbar').Fisheye(
			{
				maxWidth: 32,
				items: 'a',
				itemsText: 'span',
				container: '.tagbarcontainer',
				itemWidth: 32,
				proximity: 32,
				alignment : 'center',
				halign : 'center'
			}
		).bind('mouseover',opacifyPhotobar
		).bind('mouseout',desOpacifyPhotobar);		
	}
);
 

// test if an array contain a value
// @author : seb.attal
function contains(array, value) {
	var i = 0;
	for(i=0; i<array.length; i++){
		var iter = array[i];
		if(iter == value){
			return true;
		}
	}
	return false;
}

// Handling click on tagbar
function tagClick()
{
	if($(this).hasClass('transparent'))
		$(this).removeClass('transparent');
	else
		$(this).addClass('transparent');						
	
	var tagSelected = new Array();
	for(i=0; i< tags.length; i++)
	{
		if (!$('#'+tags[i]).hasClass('transparent')) 
		{
			tagSelected.push(tags[i]);
		}	
	}	
	map.clearOverlays();	
	showMarkers(tagSelected);
}

function loadBaseIcon()
{
	if (GBrowserIsCompatible()) {
			baseIcon = new GIcon();
			baseIcon.iconSize=new GSize(32,32);
			baseIcon.shadowSize=new GSize(56,32);
			baseIcon.iconAnchor=new GPoint(16,32);
			baseIcon.infoWindowAnchor=new GPoint(16,0);
	}
}

$().ready(loadBaseIcon);

var marker = null;

function addMarker(tagSelected,marker_name,marker_x,marker_y,marker_filename,marker_url,dontclear)
{
	if(contains(tagSelected,marker_name))
	{
		if(!dontclear)
			removeMarker();
		var point = new GLatLng(marker_x,marker_y);
		marker = new GMarker(point, {icon:new GIcon(baseIcon,marker_filename), draggable: false});
		GEvent.addListener(marker, 'click',
			function() {								
				$('#photoSlider').addClass('loader');
				$('#blogEntry').addClass('loader');
				$('#comments').addClass('loader');
				$('#addComment').addClass('loader');
					
				$('#blogEntry').jqmShow();
					
				$.ajax({
					type: 'GET',
					url:marker_url,
					dataType: 'xml',
					success: function(xml){
						loadWindows(xml);
						$('#blogEntry').removeClass('loader');
						$('#comments').removeClass('loader');
						$('#photoSlider').removeClass('loader');
						$('#addComment').removeClass('loader');
					}
				});				
			});
					
		// Adding the marker to the map
		map.addOverlay(marker);
	}
}

var markersPhotos = null;

function addMarkerPhoto(marker_x,marker_y,marker_url)
{
	if(markersPhotos == null)
		markersPhotos = new Array();
		
		var point = new GLatLng(marker_x,marker_y);
		var m = new GMarker(point, {icon:new GIcon(baseIcon,'img/photo.png'), draggable: false});
		GEvent.addListener(m, 'click',
			function() {								
				$('#photoSlider').addClass('loader');
				$('#blogEntry').addClass('loader');
				$('#comments').addClass('loader');
				$('#addComment').addClass('loader');
				
				$('#blogEntry').jqmShow();
				$('#photos').jqmShow();

				$.ajax({
					type: 'GET',
					url:marker_url,
					dataType: 'xml',
					success: function(xml){
						loadWindows(xml);
						$('#blogEntry').removeClass('loader');
						$('#comments').removeClass('loader');
						$('#photoSlider').removeClass('loader');
						$('#addComment').removeClass('loader');
					}
				});				
			});		
	markersPhotos.push(m);
}

function showMarkersPhotos()
{
	if(markersPhotos)
	{
		for(var i=0; i< markersPhotos.length; i++)
		{
			map.addOverlay(markersPhotos[i]);
		}
	}
}


function removeMarker()
{
	if(marker)
		map.removeOverlay(marker);
}